package com.eleme.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eleme.sms.SendMsg;
import com.eleme.search.SearchForEnter;


/**
 * Servlet implementation class GetTelServlet
 */
public class GetTelServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetTelServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		PrintWriter out = response.getWriter();
		String telnumber = request.getParameter("telnum");//获取前台传来的手机号
		SendMsg smsg = new SendMsg();
		try {
			System.out.println("开始执行搜索方法");
			String str = SearchForEnter.Search(telnumber);
			System.out.println("查询到的结果为"+str);
			if(str==null) {
				System.out.println("没找到这个号码");
				System.out.println("开始执行插入方法");
				SearchForEnter.Insert(telnumber);
			}else {
				System.out.println("已查询到！"+str);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//获得查询结果

		String num = smsg.send(telnumber);//电话号码发送并获取验证码
		response.getWriter().print(num);
		request.getSession().setAttribute("cellphone", telnumber);
//		out.flush();
//	    out.close();
	}
	
//	private int parestInt(String telnumber) {
//		// TODO Auto-generated method stub
//		return 0;
//	}

}
